package com.hqyj.service.impl;

import com.hqyj.dao.UserDao;
import com.hqyj.dao.impl.UserDaoImpl;
import com.hqyj.pojo.Book;
import com.hqyj.pojo.User;
import com.hqyj.service.UserService;
import com.hqyj.util.RandomSaltUtil;
import com.hqyj.util.Sha256Util;

import java.util.List;

/**
 * @author cz
 * 2021/8/11 16:08
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    /**
     * 注册用户
     * @param user
     */
    @Override
    public void registerUser(User user) {
        String password = user.getPassword();
        String salt = RandomSaltUtil.getRandomString2(16);
        String password_salt = Sha256Util.getSHA256(password + salt);
        user.setPassword(password_salt);//加盐后再添加进数据库
        user.setSalt(salt);
        userDao.addUser(user);
    }

    /**
     * 登录业务
     * @param username
     * @param password
     * @return
     */
    @Override
    public User loginUser(String username, String password) {
        User user = userDao.selectUserByName(username);
        String hash_password = user.getPassword();
        String salt = user.getSalt();
        if (Sha256Util.getSHA256(password+salt).equals(hash_password)){
            return user;
        }else {
            return null;
        }
    }

    /**
     * 查询所有图书
     * @return
     */
    @Override
    public List<Book> findBookAll() {
        return userDao.selectBookAll();
    }

    /**
     * 查询用户借阅的图书
     * @param userId
     * @return
     */
    @Override
    public List<Book> findUserBooks(int userId) {
        return userDao.selectBooksByReadId(userId);
    }

    /**
     * 返还用户借阅图书
     * @param bookId
     */
    @Override
    public void modifyBookByBookId(int bookId) {
        userDao.updateBookByBookId(bookId);
    }

    /**
     * 查询未被借阅的图书
     * @return
     */
    @Override
    public List<Book> findUnborrowBooks() {
        return userDao.selectBooksByLendedIsZero();
    }

    /**
     * 用户借阅图书
     * @param bookId
     * @param readId
     */
    @Override
    public void modifyBookByBookId(int bookId, int readId) {
        userDao.updateBookByBookId(bookId,readId);
    }

    /**
     * 管理员添加图书
     * @param book
     */
    @Override
    public void addBook(Book book) {
        userDao.insertBook(book);
    }

    /**
     * 管理员对图书进行预更新
     * @param bookId
     * @return
     */
    @Override
    public Book preModifyBook(int bookId) {
        return userDao.selectBookByBookId(bookId);
    }

    /**
     * 管理员对图书进行更新业务
     * @param book
     */
    @Override
    public void modifyBook(Book book) {
        userDao.updateBookByBookId(book);
    }

    /**
     * 管理员删除图书
     * @param bookId
     */
    @Override
    public void dropBook(int bookId) {
        userDao.deleteBook(bookId);
    }


    /**
     * 修改用户密码
     */
    @Override
    public void modifyPassword(User user,String newpass) {
        String salt = user.getSalt();
        String newpass_salt = Sha256Util.getSHA256(newpass+salt);
        user.setPassword(newpass_salt);
        userDao.updateUserPassword(user);
    }

    /**
     * 根据唯一用户名查询用户，用来修改密码
     * @param username
     * @return
     */
    @Override
    public User findUser(String username,String pass) {
        User user = userDao.selectUserByName(username);
        String salt = user.getSalt();
        String pass_salt = Sha256Util.getSHA256(pass+salt);
        if (pass_salt.equals(user.getPassword())){
            return user;
        }else {
            return null;
        }
    }
}
